package com.powernode.work.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.powernode.commons.utils.DateUtils;
import com.powernode.commons.utils.ResponseData;
import com.powernode.commons.utils.UUIDUtils;
import com.powernode.work.bean.ActivityBean;
import com.powernode.work.dao.ActivityMapper;
import com.powernode.work.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper dao;

    /**
     * 查询所有
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResponseData pageList(Integer pageNum, Integer pageSize,ActivityBean activity) {
        Page<ActivityBean> page = PageHelper.startPage(pageNum, pageSize);
        dao.selectAll(activity);
        return ResponseData.ok(page.getResult(),page.getTotal());
    }

    /**
     * 添加
     * @param activity
     * @return
     */
    @Override
    public ResponseData add(ActivityBean activity) {
        //设置主键
        activity.setId(UUIDUtils.getUUID());
        //设置创建时间
        activity.setCreateTime(DateUtils.formatDateTime(new Date()));
        //插入
        int row = dao.insertSelective(activity);
        if (row>0){
            return ResponseData.ok();
        }
        return ResponseData.error();
    }

    /**
     * 修改
     * @param activity
     * @return
     */
    @Override
    public ResponseData modify(ActivityBean activity) {
        //设置修改时间
        activity.setEditTime(DateUtils.formatDateTime(new Date()));
        int row = dao.updateByPrimaryKeySelective(activity);
        if (row>0){
            return ResponseData.ok();
        }
        return ResponseData.error();
    }

    /**
     * 删除或者批量删除
     * @param ids    1,2,3,4,5
     * @return
     */
    @Override
    public ResponseData remove(String ids) {
        int row = dao.delBatch(ids.split(","));
        if (row>0){
            return ResponseData.ok();
        }
        return ResponseData.error();
    }

    /**
     * 根据活动id查询活动的详细信息
     * @param id
     * @return
     */
    @Override
    public ResponseData findById(String id) {
        return ResponseData.ok(dao.selectByPrimaryKey(id));
    }

    /**
     * 根据线索的id查询市场活动
     * @param clueId
     * @return
     */
    @Override
    public ResponseData findActivitysByClueId(String clueId) {
        return ResponseData.ok(dao.selectActivitysByClueId(clueId));
    }

    /**
     * 解除线索市场活动的关联
     * @param clueId
     * @param activityId
     * @return
     */
    @Override
    public ResponseData removeActivityClueByActivityIdAndClueId(String clueId, String activityId) {
        int row = dao.deleteClueActivityByClueIdAndActivityId(clueId, activityId);
        if (row>0){
            return ResponseData.ok();
        }
        return   ResponseData.error();
    }

    /**
     * 统计当年的每月的活动数量
     * @return
     */
    @Override
    public ResponseData findCountGroup() {
        return ResponseData.ok(dao.selectGroup());
    }
}
